<?php
class User extends AppModel {

  var $name = 'User';
  var $validate = array(
    'name' => array(
      'notempty' => array(
        'rule' => 'notEmpty',
        'required' => TRUE,
        'message' => 'Bitte gib deinen Namen an',
      ),
    ),
    'email' => array(
      'unique' => array(
        'rule' => 'isUnique',
        'required' => TRUE,
        'message' => 'Die E-Mail-Adresse muss eindeutig sein',
      ),
      'email' => array(
        'rule' => 'email',
        'required' => TRUE,
        'message' => 'Bitte gib eine gültige E-Mail-Adresse an',
      ),
    ),
  );

  //The Associations below have been created with all possible keys, those that are not needed can be removed
  var $hasMany = array(
    'Point' => array(
      'className' => 'Point',
      'foreignKey' => 'user_id',
      'dependent' => TRUE,
      'conditions' => '',
      'fields' => '',
      'order' => 'created DESC',
      'limit' => '3',
      'offset' => '',
      'exclusive' => '',
      'finderQuery' => '',
      'counterQuery' => ''
    ),
    'LocalAccount' => array(
      'className' => 'LocalAccount',
      'foreignKey' => 'user_id',
    ),
    'OpenidAccount' => array(
      'className' => 'OpenidAccount',
      'foreignKey' => 'user_id',
    ),
    'TwitterAccount' => array(
      'className' => 'TwitterAccount',
      'foreignKey' => 'user_id',
    ),
  );
  
  function beforeSave() {
    if (isset($this->data[$this->alias]['email'])) {
      $this->data[$this->alias]['email_md5'] = md5($this->data[$this->alias]['email']);
    }
    return TRUE;
  }

  function afterFind($results) {
    foreach ($results as $i => $result) {
      if (isset($result[$this->alias]['avatar'])) {
        if ($result[$this->alias]['avatar']) {
          $uid = $result[$this->alias]['id'];
          $results[$i][$this->alias]['avatar'] = '/img/avatar/' . $uid % 10 . '/' . $uid . '.png';
        }
      }
      if (!@$result[$this->alias]['avatar'] && isset($result[$this->alias]['email'])) {
        $results[$i][$this->alias]['avatar'] = 
          'http://www.gravatar.com/avatar/' . md5($result[$this->alias]['email']) . '.jpg?s=65&d=monsterid';
      }
    }
    return $results;
  }

  function hashPasswords(&$data) {
  }

  /**
   * Merge accounts of old user user into new user.
   * Afterwards, delete old user.
   */
  function merge_users($old_uid, $new_uid) {
    foreach (array('LocalAccount', 'OpenidAccount', 'TwitterAccount', 'Point') as $model_name) {
      App::import('Model', $model_name);
      $model = new $model_name;
      $model->updateAll(array('user_id' => $new_uid), array('user_id' => $old_uid));
      if ($model_name == 'Point') {
        $model->updateAll(array('sender_id' => $new_uid), array('sender_id' => $old_uid));
      }
    }

    $old_user = $this->find('first', array('conditions' => array('id' => $old_uid)));
    $new_user = $this->find('first', array('conditions' => array('id' => $new_uid)));
    
    $data = array();
    if ($old_user['User']['name'] && !$new_user['User']['name']) {
      $data['name'] = $old_user['User']['name'];
    }
    if ($old_user['User']['email'] && !$new_user['User']['email']) {
      $data['email'] = $old_user['User']['email'];
    }
    if ($old_user['User']['created'] < $new_user['User']['created']) {
      $data['created'] = $old_user['User']['created'];
    }
    $this->set('id', $new_user['User']['id']);
    $this->save($data);
    
    $this->delete($old_uid);
  }
  
  function user_id($account) {
    $acct = current($account); 
    return isset($acct['user_id'])
      ? $acct['user_id'] // access LocalAccount, OpenidAccount etc.
      : NULL;
  }
}
